package com.zk.yygh.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.yygh.enums.OrderStatusEnum;
import com.zk.yygh.hosp.result.Result;
import com.zk.yygh.hosp.utils.AuthContextHolder;
import com.zk.yygh.model.order.OrderInfo;
import com.zk.yygh.order.service.OrderService;
import com.zk.yygh.vo.order.OrderCountQueryVo;
import com.zk.yygh.vo.order.OrderCountVo;
import com.zk.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 张凯
 * @date: 2022/6/20 - 14:48
 */
@Api(tags = "订单接口")
@RequestMapping("/api/order/orderInfo")
@RestController
public class OrderInfoApiController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("根据排班id和就诊人id生成订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(@PathVariable("scheduleId") String scheduleId, @PathVariable("patientId") Long patientId) {
        Long orderNum = orderService.saveOrder(scheduleId, patientId);
        return Result.ok(orderNum);
    }

    @ApiOperation("订单列表（订单查询带分页）")
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable("page") Integer page,
                       @PathVariable("limit") Integer limit,
                       OrderQueryVo orderQueryVo, HttpServletRequest request) {
        //给查询的实体设置id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        //封装查询参数
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        //调用业务层查询及分页
        IPage<OrderInfo> pageModel = orderService.orderInfoList(pageParam, orderQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("获取订单状态列表")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return Result.ok(statusList);
    }

    @ApiOperation("根据订单id获取订单详情")
    @GetMapping("auth/getOrder/{orderId}")
    public Result getOrder(@PathVariable("orderId") Long orderId){
        return Result.ok(orderService.getOrder(orderId));
    }

    @ApiOperation("获取订单安排日期及数量做统计")
    @PostMapping("inner/getCountMap")
    public Map<String,Object> getReserveDateMap(@RequestBody OrderCountQueryVo orderCountQueryVo){
        Map<String, Object> reserveDateMap = orderService.getReserveDateMap(orderCountQueryVo);
        return reserveDateMap;
    }
}
